package com.jin.webvideo.base.spring.resource;

import com.jin.webvideo.base.SpringBeanGetter;
import com.jin.webvideo.base.linkedfile.LinkedFile;
import com.jin.webvideo.base.linkedfile.io.LinkedFileInputStream;
import com.jin.webvideo.base.linkedfile.util.InputStreamUtils;
import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class LinkedFileResourceInputStream extends LinkedFileInputStream {
    private long length;

    public LinkedFileResourceInputStream(LinkedFile linkedFile) throws IOException {
        super(linkedFile);
    }

    public void setResourceLength(long length) {
        this.length = length;
    }

    @Override
    protected InputStream createInputStream(URI uri) throws IOException {
        if (uri.getScheme().startsWith("http")) {
            return new HttpRangeInputStream(uri, this.length, SpringBeanGetter.getBean(HttpClient.class));
        }
        else {
            return InputStreamUtils.getInputStreamByURI(uri, 3);
        }
    }
}
